package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class MyController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("testString")
    @ResponseBody
    public String testString(){
        redisTemplate.opsForValue().set("key","111");
        redisTemplate.opsForValue().set("key2","222");
        redisTemplate.opsForValue().set("key3","333");

        String value = redisTemplate.opsForValue().get("key");
        System.out.println("value:"+value);

        return "ok";
    }

    public String testList(){
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("key","111");
        redisTemplate.opsForList().leftPush("key2","222");
        redisTemplate.opsForList().leftPush("key3","333");

        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:"+value);
         value = redisTemplate.opsForList().rightPop("key2");
        System.out.println("value:"+value);
         value = redisTemplate.opsForList().rightPop("key3");
        System.out.println("value:"+value);

        return "ok";

    }

    public String testSet() {
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key","111","222","333");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println(result);

        Boolean exists = redisTemplate.opsForSet().isMember("key","111");
        System.out.println(exists);

        Long count = redisTemplate.opsForSet().size("key");
        System.out.println(count);

        redisTemplate.opsForSet().remove("key","111","222");
        result = redisTemplate.opsForSet().members("key");
        System.out.println(result);

        return "ok";

    }

    public String testHash() {
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key","f1","111");
        redisTemplate.opsForHash().put("key","f2","222");
        redisTemplate.opsForHash().put("key","f3","333");

        String value = (String) redisTemplate.opsForHash().get("key","f1");
        System.out.println(value);

        Boolean exists = redisTemplate.opsForHash().hasKey("key","f1");
        System.out.println(exists);

        redisTemplate.opsForHash().delete("key","f1","f2");

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println(size);

        return "ok";

    }

    public String testZset() {
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key","zahngsan",10);

        redisTemplate.opsForZSet().add("key","lisi",20);

        redisTemplate.opsForZSet().add("key","wangwu",30);

        Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println(members);

        Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println(membersWithScore);

        Double score = redisTemplate.opsForZSet().score("key","zhangsan");
        System.out.println(score);

        redisTemplate.opsForZSet().remove("key","zhangsan");

        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println(size);

        Long rank = redisTemplate.opsForZSet().rank("key","lisi");
        System.out.println(rank);


        return "ok";
    }




}
